package com.xkcoding.helloworld.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class DiscountContext {

    //当一个接口有多个实现类时，Spring会自动将Strategy接口的实现类注入到这个Map中，key为bean id，value值则为对应的策略实现类
    @Autowired
    private final Map<String, DiscountService> discountServiceMap = new HashMap<>();

    /**
     *
     * @param key 实现类名
     * @param params 参数
     * @return
     */
    public String execute(String key, BigDecimal price) {
        DiscountService service = discountServiceMap.get(key);
        return service.getDiscountResult(price);
    }
}
